program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  t, i, m, n: integer;
  arr: array of integer;

procedure Heapify (num: integer);
var a, b: integer;
begin
  while(1=1) do
  begin
    a:=2000000000;
    b:=2000000000;
    if (2*num+1<n) then
      a:=arr[2*num+1];
    if (2*num+2<N) then
      b:=arr[2*num+2];
    if (a<=b) and (a<arr[num]) then
    begin
      t:=arr[2*num+1];
      arr[2*num+1]:=arr[num];
      arr[num]:=t;
      num:=2*num+1;
    end else if (b<=a) and (b<arr[num]) then
    begin
      t:=arr[2*num+2];
      arr[2*num+2]:=arr[num];
      arr[num]:=t;
      num:=2*num+2;
    end else
      break;
  end;
end;

begin
  readln(n);
  setlength(arr, n);
  m:=n;
  for i:=0 to n-1 do
    read(arr[i]);
  for i:=n-1 downto 0 do
    heapify(i);
  for i:=0 to m-1 do
  begin
    t:=arr[0];
    arr[0]:=arr[n-1];
    arr[n-1]:=t;
    dec(n);
    heapify(0);
  end;
  n:=m;
  for i:=n-1 downto 0 do
    write(arr[i], '  ');
end.
